package com.doit.demo04;

import java.util.Scanner;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
@SuppressWarnings("all")
public class My_Pool {
    private static volatile boolean flag = true;

    public static void main(String[] args) throws InterruptedException {

        //此线程仅用于演示效果 可以让线程池中的线程任务快速执行完毕
        new Thread(new Runnable() {
            @Override
            public void run() {
                Scanner sc = new Scanner(System.in);
                while (sc.hasNext()) {
                    String s = sc.nextLine();
                    if ("stop".equals(s)) {
                        flag = false;
                        break;
                    }
                }
            }
        }).start();

        //创建线程池
        ThreadPoolExecutor pool = new ThreadPoolExecutor(2,
                3,
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<Runnable>(5),
                // Executors.defaultThreadFactory(),     //
                new ThreadPoolExecutor.AbortPolicy());  //抛弃不要
        //创建10个线程任务执行
        for (int i = 0; i < 10; i++) {
            Thread.sleep(1000);
            pool.execute(new MyRunnable((i + 1) + "")); //提交线程任务

        }
    }
    static class MyRunnable implements Runnable {
        private String name;

        public MyRunnable(String name) {
            this.name = name;
        }

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + "执行任务" + name);
            while (flag) {
            }
        }
    }
}
